package com.diankangwo.controller;


import com.diankangwo.constant.resultMsg;
import com.diankangwo.dao.SearchDAO;
import com.diankangwo.dao.UserDAO;
import com.diankangwo.domain.Search;
import com.diankangwo.domain.UserInfo;
import com.diankangwo.util.PageSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 搜索记录控制器
 *
 * @author xiaoyu
 */
@Controller
@Api(description = "搜索记录")
public class SearchController {
    @Autowired
    private UserDAO userDao;
    @Autowired
    private SearchDAO searchDao;

    /**
     * 加载搜索关键词列表
     *
     * @return
     */
    @ApiOperation(value = " 加载搜索关键词列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型 1：关键词列表  0：用户所有关键词历史", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "antistop", value = "关键词", required = true, dataType = "String")
    })
    @ResponseBody
    @RequestMapping(value = "/search/list", method = RequestMethod.GET)
    public String list(String token, Integer type, String antistop,int pageNo,int pageSize) {
        String code = "2";
        String msg = "处理异常！";
        PageSupport<Search> searchList = null;
        UserInfo user = userDao.getUserByToken(token);
        if (type != null && type != 0) {
            if (user != null) {
                searchList = searchDao.findBySearchInfo(user.getId(), type, antistop,pageNo,pageSize);
                if (searchList != null) {
                    code = "1";
                    msg = "查询成功！";
                } else {
                    code = "1";
                    msg = "未查询到搜索信息！";
                }
            } else {
                msg = "未查询到用户信息";
            }
        } else {
            searchList = searchDao.findBySearchInfo(null, type, antistop,pageNo,pageSize);
            if (searchList != null) {
                code = "1";
                msg = "查询成功！";
            } else {
                code = "1";
                msg = "未查询到搜索信息！";
            }
        }
        return resultMsg.mapToJson(code, msg, searchList.getResultData()== null ? null: searchList.getResultData());
    }


    /**
     * 增加或修改搜索关键词
     *
     * @param Search 搜索对象
     * @return
     */
    @ApiOperation(value = "增加或修改搜索关键词", notes = "")
    @ApiImplicitParam(name = "Search", value = "搜索对象", required = true, dataType = "Search")
    @ResponseBody
    @RequestMapping(value = "/search/saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(Search Search) {
        String code = "2";
        String msg = "处理异常！";
        if (searchDao.saveOrUpdate(Search) != null) {
            code = "1";
            msg = "操作成功！";
        } else {
            msg = "系统异常，操作失败！";
        }

        return resultMsg.mapToJson(code, msg, null);
    }

    /**
     * 删除搜索关键词
     *
     * @param id id
     * @return
     */
    @ApiOperation(value = "增加或修改搜索关键词", notes = "")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @ResponseBody
    @RequestMapping(value = "/search/del", method = RequestMethod.POST)
    public String del(Integer id) {
        String code = "2";
        String msg = "处理异常！";
        try {
            code = "1";
            searchDao.deleteById(id);
            msg = "操作成功！";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "系统异常，操作失败！";
        }
        return resultMsg.mapToJson(code, msg, null);
    }
}
